from django.db import models
from django.contrib.auth.models import AbstractUser
from django.urls import reverse
# from DreamLife.utils import get_current_site

from django.conf import settings


class User(AbstractUser):
    username = models.CharField("Account", max_length=16, unique=True)
    # 继承自抽象类的User已经包含pwd
    # password = models.CharField("Password", max_length=20)
    email = models.EmailField("Email", unique=True)
    nickname = models.CharField("Nickname", max_length=24)
    registerTime = models.DateTimeField(auto_now_add=True)
    email = models.CharField("email", max_length=50, unique=True)

    class Meta:
        ordering = ['-username']
        verbose_name = "用户"
        verbose_name_plural = "用户"
        get_latest_by = 'username'

    '''
    设置 view_on_site 来控制是否显示 “在站点上查看” 链接。这个链接应该把你带到一个可以显示保存对象的 URL。

这个值可以是一个布尔标志，也可以是一个可调用对象。如果 True （默认），对象的 get_absolute_url() 方法将被用来生成网址。
    '''
    def get_absolute_url(self):
        return reverse(
            'blog:author_detail', kwargs={
                'author_name': self.username})

    # def get_full_url(self):
    #     site = get_current_site().domain
    #     url = "https://{site}{path}".format(site=site,
    #                                         path=self.get_absolute_url())
    #     return url

    def __str__(self):
        # user_info = f"Username: {self.username}, Nickname: {self.nickname}, Registered: {self.registerTime}"
        # return user_info
        return self.username

    # Django 在没有 create_user 函数时，默认从 user_manage 中创建默认的 create_user 函数


class Follow(models.Model):
    rid = models.AutoField(primary_key=True)
    # From 关注 To
    objects = models.Manager()
    from_user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        related_name='following',  # 反向关联到 其关注者
        # # 获取用户的关注者列表（使用 related_name='following'）
        # user.following.all()
        on_delete=models.CASCADE
    )
    to_user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        related_name='followers',  # 反向关联到 其被关注者
        # # 获取用户的被关注者列表（使用 related_name='followers'）
        # user.followers.all()
        on_delete=models.CASCADE
    )

    class Meta:
        unique_together = ('from_user', 'to_user')  # 避免重复
        verbose_name = "关注表"
        verbose_name_plural = "关注表"
